<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据维护</el-breadcrumb-item>
      <el-breadcrumb-item>大修轮次管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入需要查询大修的关键字"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            size="mini"
            @keypress.enter.native="getOutages"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOutages" size="mini"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3"><el-button type="primary" size="mini" @click="addOutage">添加大修</el-button></el-col>
      </el-row>

      <!-- 表格展示区域 -->
      <el-table :data="outages" border stripe style="width: 100%" size="mini">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="outage" label="大修轮次"> </el-table-column>
        <el-table-column prop="start_date" label="开始日期"> </el-table-column>
        <el-table-column prop="end_date" label="结束日期"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="editOutage(scope.row)"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteOutage(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加大修轮次的对话框 -->
      <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="500px" @close="dialogClose">
        <el-form :model="outage" label-width="80px" ref="outageFormRef" :rules="outageFormRules">
          <el-form-item label="大修轮次" prop="outage">
            <el-input v-model="outage.outage" :disabled="dialogMode === 'add' ? false : true"></el-input>
          </el-form-item>
          <el-form-item label="开始时间" prop="startDate">
            <el-date-picker v-model="outage.startDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"> </el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期" prop="endDate">
            <el-date-picker v-model="outage.endDate" type="date" placeholder="选择日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmBtnClick">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var checkOutage = (rule, value, callback) => {
      const reg = /^[A-Z]{2}[0-9]{3}$/
      if (!reg.test(value)) {
        return callback(new Error('输入的大修轮次格式错误'))
      }
      callback()
    }
    var checkStartDate = (rule, value, callback) => {
      if (this.outage.endDate) {
        if (value >= this.outage.endDate) {
          return callback(new Error('大修开始日期应该在结束日期之前'))
        }
      }
      callback()
    }
    var checkEndDate = (rule, value, callback) => {
      if (this.outage.startDate) {
        if (value <= this.outage.startDate) {
          return callback(new Error('大修开始日期应该在结束日期之前'))
        }
      }
      callback()
    }
    return {
      queryInfo: {
        query: ''
      },
      outages: [],
      outage: {
        outage: '',
        startDate: null,
        endDate: null
      },
      dialogFormVisible: false,
      dialogTitle: '',
      outageFormRules: {
        outage: [
          { required: true, message: '请输入大修轮次', trigger: 'blur' },
          { validator: checkOutage, trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '请选择大修开始时间', trigger: 'blur' },
          { validator: checkStartDate, trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '请选择大修结束时间', trigger: 'blur' },
          { validator: checkEndDate, trigger: 'blur' }
        ]
      },
      dialogMode: 'add'
    }
  },
  created() {
    this.getOutages()
  },
  methods: {
    //   获取大修轮次信息
    async getOutages() {
      const { data: res } = await this.$http.get('baseinfo/getoutages/', { params: this.queryInfo })
      if (res.meta.status === 200) {
        this.outages = res.data.outages
        return
      }
      return this.$message.error('获取大修轮次查询结果失败！')
    },
    // 删除大修轮次
    async deleteOutage(outage) {
      const confirmResult = await this.$confirm('此操作将永久删除大修信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$http.delete('baseinfo/deleteoutage/' + outage.id)
      if (res.meta.status === 200) {
        this.$message.success('大修轮次删除成功')
        this.getOutages()
        return
      }
      this.$message.error('大修轮次删除失败！')
      this.getOutages()
    },
    // 添加大修轮次
    addOutage() {
      this.dialogMode = 'add'
      this.dialogTitle = '添加大修轮次'
      this.dialogFormVisible = true
    },
    // 点击编辑大修轮次
    editOutage(outage) {
      this.dialogMode = 'edit'
      this.dialogTitle = '编辑大修轮次'
      this.outage.outage = outage.outage
      this.outage.startDate = new Date(Date.parse(outage.start_date.replace(/-/g, '/')))
      this.outage.endDate = new Date(Date.parse(outage.end_date.replace(/-/g, '/')))
      this.dialogFormVisible = true
    },
    // 添加/编辑大修轮次的对话框关闭时重置表单
    dialogClose() {
      this.$refs.outageFormRef.resetFields()
    },
    //点击对话框的确认按钮
    confirmBtnClick() {
      this.$refs.outageFormRef.validate(async valid => {
        if (!valid) return
        if (this.dialogMode === 'add') {
          const { data: res } = await this.$http.post('baseinfo/addoutage/', this.outage)
          if (res.meta.status === 200) {
            this.$refs.outageFormRef.resetFields()
            this.dialogFormVisible = false
            this.getOutages()
            return this.$message.success('添加大修轮次成功！')
          } else if (res.meta.status === 201) {
            return this.$message.info(`${this.outage.outage}大修在数据库中已经存在，请勿重复录入！`)
          }
          return this.$message.error('添加大修过程发生错误，请联系管理员')
        } else {
          const { data: res } = await this.$http.put('baseinfo/editoutage/', this.outage)
          if (res.meta.status === 200) {
            this.dialogFormVisible = false
            this.getOutages()
            return this.$message.success('修改大修信息成功！')
          } else if (res.meta.status === 201) {
            return this.$message.info('未找到要修改的大修信息！(可能已经被删除)')
          }
          return this.$message.error('修改大修信息失败，请联系管理员')
        }
      })
    }
  },
  computed: {
    // 保证选择的大修结束日期在开始日期之后
    pickerOptions: function() {
      let that = this
      return {
        disabledDate(time) {
          if (that.outage.startDate) {
            return time.getTime() < that.outage.startDate
          }
          return false
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-date-editor {
  width: 380px;
}
</style>
